package rongyao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class test3 {
    // 保存映射
    public static Map<Integer, Integer> map = new TreeMap<>();
    static {
        for (int i = 1; i <= 20; i++) {
            map.put(i, (i - 1) * 50);
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            String[] split = s.split(":");
            int code = Integer.parseInt(split[0]);
            String content = split[1];
            switch (code) {
                case 1 :
                    int key = Integer.parseInt(content.substring(content.length() - 2));
                    System.out.println(map.get(key));
                    break;
                case 2 :
                    System.out.println(map.get(getNext(content)));
                    break;
                case 3 :
                    String[] split1 = content.split(";");
                    removeNode(split1[0]);
                    System.out.println(map.get(getNext(split1[1])));
                    break;
                case 4 :
                    System.out.println(addNode(content));
                    break;
                case 5 :
                    String[] split2 = content.split(";");
                    addNode(split2[0]);
                    System.out.println(map.get(getNext(split2[1])));
                    break;
            }
        }
    }

    public static int getHash(String token) {
        int sum = 0;
        for (char c : token.toCharArray()) {
            sum += c;
        }
        return sum % 999;
    }

    public static void removeNode(String token) {
        // 所有要移除的节点
        String[] ns = token.split(",");
        for (String n : ns) {
            int key = Integer.parseInt(n.substring(n.length() - 2));
            map.remove(key);
        }
        int count = map.size();
        int cur = 0;
        for (int key : map.keySet()) {
            map.put(key, cur);
            cur += 1000 / count;
        }

    }

    public static int addNode(String token) {
        int k = Integer.parseInt(token.substring(token.length() - 2));
        map.put(k, 0);
        int count = map.size();
        int cur = 0;
        for (int key : map.keySet()) {
            map.put(key, cur);
            cur += 1000 / count;
        }
        return map.get(k);
    }

    public static int getNext(String token) {
        int hash = getHash(token);
        for (int key : map.keySet()) {
            if (map.get(key) >= hash) return key;
        }
        for (int key : map.keySet()) {
            return key;
        }
        return 1;
    }
}
